<?php
	if ($_POST)
	{
		require('config.php');
		$videoID = $_POST["videoID"];
        $listname = $_POST["listname"];
        $newlist = $_POST["newlist"];
		if (isset($_COOKIE['userid']) && $_COOKIE['userid'] != '')
		{
			$time = date("Y-m-d H:i:s");
            if ($newlist == 1)
            {
                $stmt = $connection->prepare('insert into user_fav_lists (user_id, fav_type, create_time) values (?, ?, ?)');
                $stmt->bind_param("sss", $_COOKIE['userid'], $listname, $time);
                $stmt->execute();
                if ($stmt->affected_rows != 1)
                {
                    echo "LISTEXIST";
                    return;
                }
            }
			if ($videoID != '')
			{
                /*$stmt = $connection->prepare('update user_fav set fav_time = ? where fav_type = ? and  user_id = ? and video_id = ? limit 1');
                $stmt->bind_param("ssss", $time, $listname, $_COOKIE['userid'], $videoID);
                $stmt->execute();
                if ($stmt->affected_rows == 1)
                {
                    echo 'true';
                }
                else
                {*/
					$stmt = $connection->prepare("insert into user_fav (user_id, video_id, fav_time, fav_type) values (?, ?, ?, ?)");
					$stmt->bind_param("ssss", $_COOKIE['userid'], $videoID, $time, $listname);
					$stmt->execute();
                    if ($fav_type != '所有收藏')
                    {
                        $listname = "所有收藏";
					    $stmt = $connection->prepare("insert into user_fav (user_id, video_id, fav_time, fav_type) values (?, ?, ?, ?)");
					    $stmt->bind_param("ssss", $_COOKIE['userid'], $videoID, $time, $listname);
					    $stmt->execute();
                    }
					echo 'true';
                //}
			}
			else
			{
				echo "NOVIDEOID";
			}
		}
		else
		{
			echo "NOUSER";	
		}
        $connection->close();
	}
	else
	{
		echo "NOPOST";	
	}
?>
